﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib.MyControls;
using DAL;
using WFOffice2007;
using Microsoft.Office.Interop.Excel;

namespace 发电机舱
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        ToolStripDateTimePicker startTime;
        ToolStripDateTimePicker endTime;
        private void Form4_Load(object sender, EventArgs e)
        {
            startTime = new ToolStripDateTimePicker();
            endTime = new ToolStripDateTimePicker();
            toolStrip1.Items.Insert(4, startTime);
            toolStrip1.Items.Insert(7, endTime);
            startTime.Control.Format = DateTimePickerFormat.Custom;
            startTime.Control.CustomFormat = "yyyy-MM-dd HH:mm:ss";
            endTime.Control.Format = DateTimePickerFormat.Custom;
            endTime.Control.CustomFormat = "yyyy-MM-dd HH:mm:ss";
            startTime.Control.Value = DateTime.Now.AddDays(-1);
            toolStripComboBox1.SelectedIndex = 0;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            DateTime st = startTime.Value;
            DateTime et = endTime.Value;
            string tableName="";
            switch (toolStripComboBox1.SelectedIndex)
            {
                case 0:
                    tableName = "fdj1data";
                    break;
                case 1:
                    tableName = "fdj2data";
                    break;
                case 2:
                    tableName = "fdj1Alarm";
                    break;
                case 3:
                    tableName = "fdj2Alarm";
                    break;
            }
            System.Data.DataTable dt = fdjDataDBOption.DataTableSelect(tableName, st, et);
            dataGridView1.DataSource = dt;
        }
        System.Data.DataTable eDT;
        int eType;
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            DateTime st = startTime.Value;
            DateTime et = endTime.Value;
            string tableName = "";
            switch (toolStripComboBox1.SelectedIndex)
            {
                case 0:
                    tableName = "fdj1data";
                    break;
                case 1:
                    tableName = "fdj2data";
                    break;
                case 2:
                    tableName = "fdj1Alarm";
                    break;
                case 3:
                    tableName = "fdj2Alarm";
                    break;
            }
            eType = toolStripComboBox1.SelectedIndex;
            eDT = fdjDataDBOption.DataTableSelect(tableName, st, et);
            ExcelExport ExcelEx;
            ExcelEx = new ExcelExport(eDT.Rows.Count);
            ExcelEx.ExcelWorkbookCallbackProc = new ExcelExport.ExcelWorkbookCallback(ExcelWorkbookCallbackProc);
            ExcelEx.ExcelExportProc();
        }
        bool ExcelWorkbookCallbackProc(Workbook wBook, int index)
        {
            Worksheet wSheet;
            //wBook.Worksheets.Add();            
            Range dr;
            if (index == -1)
            {
                for (int i = 0; i < wBook.Worksheets.Count - 1; i++)
                {
                    wSheet = (Worksheet)wBook.Worksheets[i + 1];
                    wSheet.Delete();
                }                
                wSheet = (Worksheet)wBook.Worksheets[1];//2
                switch (eType)
                {
                    case 0:
                        wSheet.Name = "柴油发电机组1记录数据";
                        break;
                    case 1:
                        wSheet.Name = "柴油发电机组2记录数据";
                        break;
                    case 2:
                        wSheet.Name = "柴油发电机组1报警数据";
                        break;
                    case 3:
                        wSheet.Name = "柴油发电机组2报警数据";
                        break;
                }
                wSheet.Cells[1, 1] = "生成时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                wSheet.Cells[2, 1] = "时间";
                wSheet.Cells[2, 2] = "柴油机转速";
                wSheet.Cells[2, 3] = "润滑油压力";
                wSheet.Cells[2, 4] = "燃油压力";
                wSheet.Cells[2, 5] = "冷却水压力";
                wSheet.Cells[2, 6] = "冷却水温度";
                wSheet.Cells[2, 7] = "润滑油温度";
                wSheet.Cells[2, 8] = "1缸排气温度";
                wSheet.Cells[2, 9] = "2缸排气温度";
                wSheet.Cells[2, 10] = "3缸排气温度";
                wSheet.Cells[2, 11] = "4缸排气温度";
                wSheet.Cells[2, 12] = "5缸排气温度";
                wSheet.Cells[2, 13] = "6缸排气温度";
                wSheet.Cells[2, 14] = "AB相线电压";
                wSheet.Cells[2, 15] = "CA相线电压";
                wSheet.Cells[2, 16] = "BC相线电压";
                wSheet.Cells[2, 17] = "A相电流";
                wSheet.Cells[2, 18] = "B相电流";
                wSheet.Cells[2, 19] = "C相电流";
                wSheet.Cells[2, 20] = "频率";
                wSheet.Cells[2, 21] = "功率";
                wSheet.Cells[2, 22] = "功率因数";
                dr = wSheet.get_Range("A2", "V2");
                dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkOrange);
                dr.Interior.Pattern = XlPattern.xlPatternSolid;
                //dr.NumberFormatLocal = "0.0";
                //dr.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                //dr = wSheet.get_Range("A2", "V2");
                //                 dr = wSheet.get_Range("A3", "B124");
                //                 dr.NumberFormatLocal = "0000";
                //                 dr.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                return true;

            }
            else if (index == int.MaxValue)
            {
                wSheet = (Worksheet)wBook.Worksheets[1];
                dr = wSheet.get_Range("A1", "V" + (eDT.Rows.Count + 2).ToString());
                dr.Columns.AutoFit();
                dr.HorizontalAlignment = XlHAlign.xlHAlignCenter;
                dr.Borders.LineStyle = XlLineStyle.xlContinuous;

                dr = wSheet.get_Range("B3", "B" + (eDT.Rows.Count + 2).ToString());
                dr.NumberFormatLocal = "0.0";
                dr = wSheet.get_Range("C3", "E" + (eDT.Rows.Count + 2).ToString());
                dr.NumberFormatLocal = "0.000";
                dr = wSheet.get_Range("f3", "V" + (eDT.Rows.Count + 2).ToString());
                dr.NumberFormatLocal = "0.0";
                return true;
            }
            else 
            {
                wSheet = (Worksheet)wBook.Worksheets[1];//门限1原始数据
                wSheet.Cells[3 + index, 1] = eDT.Rows[index]["GetTime"].ToString();
                wSheet.Cells[3 + index, 2] = eDT.Rows[index]["ZS"].ToString();
                wSheet.Cells[3 + index, 3] = eDT.Rows[index]["JYYL"].ToString();
                wSheet.Cells[3 + index, 4] = eDT.Rows[index]["RYYL"].ToString();
                wSheet.Cells[3 + index, 5] = eDT.Rows[index]["LQSYL"].ToString();
                wSheet.Cells[3 + index, 6] = eDT.Rows[index]["LQSWD"].ToString();
                wSheet.Cells[3 + index, 7] = eDT.Rows[index]["RHYWD"].ToString();
                wSheet.Cells[3 + index, 8] = eDT.Rows[index]["PQWD1"].ToString();
                wSheet.Cells[3 + index, 9] = eDT.Rows[index]["PQWD2"].ToString();
                wSheet.Cells[3 + index, 10] = eDT.Rows[index]["PQWD3"].ToString();
                wSheet.Cells[3 + index, 11] = eDT.Rows[index]["PQWD4"].ToString();
                wSheet.Cells[3 + index, 12] = eDT.Rows[index]["PQWD5"].ToString();
                wSheet.Cells[3 + index, 13] = eDT.Rows[index]["PQWD6"].ToString();
                wSheet.Cells[3 + index, 14] = eDT.Rows[index]["Ua"].ToString();
                wSheet.Cells[3 + index, 15] = eDT.Rows[index]["Ub"].ToString();
                wSheet.Cells[3 + index, 16] = eDT.Rows[index]["Uc"].ToString();
                wSheet.Cells[3 + index, 17] = eDT.Rows[index]["Ia"].ToString();
                wSheet.Cells[3 + index, 18] = eDT.Rows[index]["Ib"].ToString();
                wSheet.Cells[3 + index, 19] = eDT.Rows[index]["Ic"].ToString();
                wSheet.Cells[3 + index, 20] = eDT.Rows[index]["freq"].ToString();
                wSheet.Cells[3 + index, 21] = eDT.Rows[index]["Power"].ToString();
                wSheet.Cells[3 + index, 22] = eDT.Rows[index]["allQ"].ToString();
                if (index % 2 == 1)
                {
                    wSheet = (Worksheet)wBook.Worksheets[1];
                    dr = wSheet.get_Range("A"+(3+index).ToString(), "V" + (3+index).ToString());
                    dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
                    dr.Interior.Pattern = XlPattern.xlPatternSolid;
                }
                return true;
            }
        }
    }
}
